package com.cos.students.services.impl;

import com.cos.students.common.ResponseResult;
import com.cos.students.dao.RcStudentCourseMapper;
import com.cos.students.entity.RcStudentCourse;
import com.cos.students.entity.RcTeacher;
import com.cos.students.dao.RcTeacherMapper;
import com.cos.students.services.IRcTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cos.students.vo.input.SearchChoseCourseVO;
import com.cos.students.vo.output.StudentScoreVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cos
 * @since 2022-10-24
 */
@Service
public class RcTeacherServiceImpl extends ServiceImpl<RcTeacherMapper, RcTeacher> implements IRcTeacherService {
    @Autowired
    private RcTeacherMapper teacherMapper;
    @Autowired
    private RcStudentCourseMapper sc;

    @Override
    public ResponseResult addScore(RcStudentCourse course) {
        RcStudentCourse studentCourse = sc.selectById(course.getScId());
        if (studentCourse == null) {
            return ResponseResult.createFail("没有这个课程,无法设置分数,请重新再试");
        }
        // 价格验证 要打分的这个数据的课程是不是和打分老师属于一个院系
        // 使用Beanutils  把前端查询的数据赋值给studentCourse这个对象的同名属性上
        BeanUtils.copyProperties(course, studentCourse);
        // 修改分数  使用的对象是查询出来的对象
        if (sc.updateById(studentCourse) > 0) {
          return   ResponseResult.createSuccess("打分成功");
        }
        return ResponseResult.createSuccess("打分失败");
    }

    @Override
    public ResponseResult searchStudentScore(SearchChoseCourseVO vo) {
        PageHelper.startPage(vo.getIndex(), 8);
        List<StudentScoreVo> studentScoreVos = teacherMapper.searchStudentScore(vo);
        PageInfo<StudentScoreVo> info = new PageInfo<>(studentScoreVos);
        return ResponseResult.createSuccess(info);
    }

    @Override
    public ResponseResult showTeacherCourseTimeTable(Integer userId) {
        return ResponseResult.createSuccess(teacherMapper.showTeacherCourseTimeTable(userId));
    }

    @Override
    public ResponseResult getTeacherCourse(Integer userId) {
        RcTeacher rcTeacher = teacherMapper.selectById(userId);
        if (rcTeacher == null) {
            return ResponseResult.createFail("该教员不存在");
        }
        return ResponseResult.createSuccess(teacherMapper.getTeacherCourse(userId));
    }
}
